<template>
<view>
	<view class="header">
	    <input @input="search" placeholder="输入物品名称"/>
	</view>
	<view class="select">
		<view @click="select(1)" class="select_line border-right" :class="{'border-bottom':type == 1}">院线产品</view>
		<view @click="select(2)" class="select_line" :class="{'border-bottom':type == 2}">家居产品</view>
	</view>
	<view class="content">
	    <view class="list" v-for="(item,index) in list" :key="index">
			<view class="list_init">
				<view class="list_title">{{item.name}}</view>
				<image class="list_img" v-if="oaRole == 1 || oaRole == 3"
				@click="frame('stockEditShow',item)" src="/static/img/none_edit.jpg"></image>
			</view>
			<view class="list_init">
				<view style="color:#007AFF;">{{item.type == 1 ? '院线产品' : '家居产品'}}</view>
				<view>库存数量:{{item.num}}</view>
				<image class="list_img1" @click="del(item.id)" v-if="oaRole == 1 || oaRole == 3"
				src="/static/img/none_delete.png"></image>
			</view>
			<view class="list_init">
				<view>价格:{{item.price}}</view>
				<view v-if="item.type == 2" class="sale" @click="frame('stockSaleShow',item)">
					购买
				</view>
			</view>
	    </view>
	</view>
	
	<button v-if="oaRole == 1 || oaRole == 3" @click="frame('stockAddShow',0)" class="addButton">添加商品</button>
	
	<stockEdit :item="item" v-if="stockEditShow" @func="close"></stockEdit>
	<stockAdd v-if="stockAddShow" @func="close"></stockAdd>
	<stockSale :item="item" v-if="stockSaleShow" @func="close"></stockSale>
</view>
</template>
<script>
import request from '@/util/request.js'
import stockEdit from "@/components/system/stockEdit.vue"
import stockAdd from "@/components/system/stockAdd.vue"
import stockSale from "@/components/system/stockSale.vue"
export default {
	data() {
		return {
			//oaRole 1:店长2:员工3:商户
			oaRole:0,
			list:[],
			keyword:'',
			imgUrl:this.url,
			stockEditShow:false,
			stockAddShow:false,
			item:{},
			type:1,
			stockSaleShow:false,
			page:1,
			pageShow:true
		}
	},
	components:{stockAdd,stockEdit,stockSale},
	onLoad:function(){
		uni.showLoading({
		    title: '加载中'  
		});
		this.getList();
		this.oaRole = uni.getStorageSync('oaRole')
	},
	methods: {
		getList:function(){
		    var data = {
		        oa_id:uni.getStorageSync('oa_id'),
		        name:this.keyword,
				type:this.type,
				page:this.page
		    }
		    var that = this
			request({
				url: 'manage/goods/list',
				method: 'get',
				data:data,
				success: (res) => {
					uni.hideLoading();
					if(res.data.code == 1){
						if(res.data.data.last_page <= that.page){
							that.pageShow = false
						}
						if(that.page == 1){
							that.list = res.data.data.data
						}else{
							var list = that.list
							that.list = list.concat(res.data.data.data)
						}
					}
				},
			})
		},
		onReachBottom: function(){
			if(this.pageShow){
				 //当前不是最后一页可加载
				 this.page = this.page + 1
				 uni.showLoading()
				 this.getList();
			}
		},
		search:function(e){
			this.keyword = e.detail.value
			this.page = 1
			this.pageShow = true
			uni.showLoading({
			    title: '加载中'  
			});
			this.getList();
		},
		frame:function(key,item){
			this.item = item
			this[key] = true
		},
		close:function(data){
			this[data.model] = false
			if(data.type){
				uni.showLoading({
				    title: '加载中'  
				});
				this.getList();
			}
		},
		select:function(key){
			this.type = key;
			this.page = 1
			this.pageShow = true
			uni.showLoading({
			    title: '加载中'  
			});
			this.getList();
		},
		del:function(id){
			var that = this
			uni.showModal({
				title: '提示',
				content: '是否要删除吗?',
				success: function (res) {
					if (res.confirm) {
						that.putGoods(id)
					}else if (res.cancel) {
						//console.log('用户点击取消');
					}
				}
			});
		},
		putGoods:function(id){
			var data = {id:id}
			var that = this
			request({
				url: 'manage/goods/delete',
				method: 'delete',
				data:data,
				success: (res) => {
					if(res.data.code == 1){
						uni.showToast({
							title: '删除成功!!!',
							icon: "none", 
							position:'bottom',
						});
						that.getList()
					}
				},
			})
		}
	}
}
</script>
<style>
@import url("../css/goods/index.css");
</style>